package api;

import beanInfo.MeaningInfo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import http.RequestSender;
import http.Response;
import model.ApiResultModel;
import model.SearchResultModel;
import myException.ServerConnectException;

import java.io.IOException;
import java.util.*;

/**
 * 服务端DictionaryAction的方法调用
 */
public class DictionaryApi {

    public final static String DICTIONARY_ACTION_PATH = "/dictionary";

    public final static String ADD = DICTIONARY_ACTION_PATH + "/add";
    public final static String DELETE_WORD = DICTIONARY_ACTION_PATH + "/delete_word";
    public final static String DELETE_MEANING = DICTIONARY_ACTION_PATH + "/delete_meaning";
    public final static String SEARCH_WORD = DICTIONARY_ACTION_PATH + "/search_word";
    public final static String GET_NEW_WORDS = DICTIONARY_ACTION_PATH + "/get_new_words";
    public final static String UPDATE_MEANING = DICTIONARY_ACTION_PATH + "/update_meaning";
    public final static String GET_SEARCH_HINT = DICTIONARY_ACTION_PATH + "/get_search_hint";
    public final static String GET_RANDOM_WORD = DICTIONARY_ACTION_PATH + "/get_random_word";

    /**
     * 新增单词或单词含义
     */
    public static Response add(long user_id, int add_mode, String word_string, List<String> meaningList) throws ServerConnectException {
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("user_id", String.valueOf(user_id));
        paramsMap.put("add_mode", String.valueOf(add_mode));
        paramsMap.put("word_string", word_string);
        paramsMap.put("meanings", String.join(MeaningInfo.SEPARATOR, meaningList));
        return RequestSender.sendPostRequest(ADD, JSON.toJSONString(paramsMap));
    }

    /**
     * 删除一个单词
     */
    public static Response delete_word(long user_id, long word_id) throws ServerConnectException {
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("user_id", String.valueOf(user_id));
        paramsMap.put("word_id", String.valueOf(word_id));
        return RequestSender.sendPostRequest(DELETE_WORD, JSON.toJSONString(paramsMap));
    }

    /**
     * 删除一个单词释义
     */
    public static Response delete_meaning(long user_id, long meaning_id) throws ServerConnectException {
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("user_id", String.valueOf(user_id));
        paramsMap.put("meaning_id", String.valueOf(meaning_id));
        return RequestSender.sendPostRequest(DELETE_MEANING, JSON.toJSONString(paramsMap));
    }

    /**
     * 用word_id或者word_string查找单词
     */
    public static Response search_word(long user_id, long word_id, String word_string) throws ServerConnectException {
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("user_id", String.valueOf(user_id));
        paramsMap.put("word_id", String.valueOf(word_id));
        paramsMap.put("word_string", word_string);
        return RequestSender.sendPostRequest(SEARCH_WORD, JSON.toJSONString(paramsMap));
    }

    /**
     * 获取最近新增单词列表
     */
    public static Response get_new_words(int num) throws ServerConnectException {
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("num", String.valueOf(num));
        return RequestSender.sendPostRequest(GET_NEW_WORDS, JSON.toJSONString(paramsMap));
    }

    public static Response update_meaning(long user_id, long meaning_id, String update_meaning) throws ServerConnectException {
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("user_id", String.valueOf(user_id));
        paramsMap.put("meaning_id", String.valueOf(meaning_id));
        paramsMap.put("update_meaning", update_meaning);
        return RequestSender.sendPostRequest(UPDATE_MEANING, JSON.toJSONString(paramsMap));
    }

    public static Response get_search_hint(long user_id, int num, String prefix) throws ServerConnectException {
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("user_id", String.valueOf(user_id));
        paramsMap.put("num", String.valueOf(num));
        paramsMap.put("prefix", prefix);
        return RequestSender.sendPostRequest(GET_SEARCH_HINT, JSON.toJSONString(paramsMap));
    }

    public static Response get_random_word() throws ServerConnectException {
        Map<String, String> paramsMap = new HashMap<>();
        return RequestSender.sendPostRequest(GET_RANDOM_WORD, JSON.toJSONString(paramsMap));
    }

}
